package com.unionftech.common;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;

import com.alibaba.android.arouter.launcher.ARouter;
import com.unionftech.common.permissions.PermissionUtils;
import com.unionftech.common.permissions.tip.impl.DefaultTipsShowController;
import com.unionftech.common.permissions.tip.impl.adapter.RawPermissionTipInfoAdapter;
import com.unionftech.common.utils.LogUtils;
import com.unionftech.common.utils.LogWatcher;
import com.unionftech.common.utils.SpUtils;
import com.unionftech.common.webview.WebViewPool;

import skin.support.SkinCompatManager;
import skin.support.app.SkinAppCompatViewInflater;
import skin.support.app.SkinCardViewInflater;
import skin.support.constraint.app.SkinConstraintViewInflater;
import skin.support.design.app.SkinMaterialViewInflater;

public class CommonInit {
    @SuppressLint("StaticFieldLeak")
    private volatile static CommonInit instance = null;
    private Application mContext;

    private CommonInit() {
    }

    public static CommonInit getInstance() {
        if (instance == null) {
            synchronized (CommonInit.class) {
                if (instance == null) {
                    instance = new CommonInit();
                }
            }
        }
        return instance;
    }

    public void init(Application context, boolean isDebug) {
        this.mContext = context;
        //日志初始化
        LogUtils.init(context, isDebug);
        //sp初始化
        SpUtils.getInstance().init(context);
        if (isDebug) {
            ARouter.openLog();
            ARouter.openDebug();
        }
        ARouter.init(context);
        WebViewPool.getInstance().init(mContext);
        //权限申请框架初始化
        RawPermissionTipInfoAdapter tipInfoAdapter = new RawPermissionTipInfoAdapter(mContext,
                R.raw.permission_tip_descriotion);
        DefaultTipsShowController showController = new DefaultTipsShowController(tipInfoAdapter);
        PermissionUtils.init(showController);
        LogWatcher.getInstance().init(mContext, "log").startWatch();
        //换肤
        SkinCompatManager.withoutActivity(mContext)
                .addInflater(new SkinAppCompatViewInflater()) // 基础控件换肤初始化
                .addInflater(new SkinMaterialViewInflater())  // material design 控件换肤初始化[可选]
                .addInflater(new SkinConstraintViewInflater()) // ConstraintLayout 控件换肤初始化[可选]
                .addInflater(new SkinCardViewInflater())    // CardView v7 控件换肤初始化[可选]
                .loadSkin();
    }

    public Context getContext() {
        return this.mContext;
    }

    public void destroy() {
        ARouter.getInstance().destroy();
        WebViewPool.getInstance().destroy();
        LogWatcher.getInstance().stopWatch();
    }
}
